package com.zw.design.pattern.creational.singleton.hungry;

import java.io.*;

/****
 * 序列化破坏单例模式
 */
public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {

        HungryLazySingleton hungryLazySingleton=HungryLazySingleton.getInstance();
        //序列化
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("singleton_file"));
        oos.writeObject(hungryLazySingleton);
        //读取操作
        File file=new File("singleton_file");
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
        HungryLazySingleton newLazySingleton  = (HungryLazySingleton) ois.readObject();
        System.out.println("newLazySingleton = " + newLazySingleton);
        System.out.println("hungryLazySingleton = " + hungryLazySingleton);
        //输出两个对象是否是相等的？
        System.out.println(newLazySingleton==hungryLazySingleton);
    }
}
